
function getRecycleData() {
    $.ajax({
        type: 'get',
        url: 'getRecycleData',
        success:function (data) {
            $tbody = $("#tbody");
            for (let i = 0;i<data.length;i++){
                $tr = $("<tr></tr>");
                $tr.appendTo($tbody);
                $tr.append("<td>"+data[i].name+"</td>");
                $tr.append("<td>"+data[i].weight+"KG"+"</td>");
                $tr.append("<td>"+data[i].clothes+"件"+"</td>");
                $tr.append("<td>"+data[i].books+"本"+"</td>");
                $tr.append("<td>"+data[i].address+"</td>");
                $tr.append("<td>"+data[i].time+"</td>");
            }
            tableScroll('tableId', 250, 25, 5);
        }

    })

}

function getData() {
    $.ajax({
        type: 'get',
        url: 'getData',
        success:function (data) {
            const totalAmount = data.totalAmount.toFixed(2);
            const totalBooksNumber = data.totalBooksNumber;
            const totalWeight = data.totalWeight;
            const totalUsers = accounting.formatMoney(data.totalUsers, '', 0);
            $("#totalAmount").text(accounting.formatMoney(totalAmount,'',0));
            $("#totalBooksNumber").text(accounting.formatMoney(totalBooksNumber,'',0));
            $("#totalUsers").text(totalUsers);
            $("#totalWeight").text(accounting.formatMoney(totalWeight,'',0));
        }
    })
}
var time;
$(function () {
    time = setInterval(getData,200);
});

layui.use('element', function () {
    var element = layui.element;
});
// layui.use('layer', function(){
//     var layer = layui.layer;
//     layer.open({
//         type: 2,
//         area: ['70%','80%'],
//         title: '就近找到我们',
//         offset: 'auto',
//         content: ['http://dd.ddandang.top/html/index.html','no']
//     });
// });

let url = "http://dd.ddandang.top/html/index.html";
layui.use('layer', function() {
    var flag = false;
    var layer = layui.layer;
    //判断用户ip是否已经登录过
    $.ajax({
        type: 'get',
        url: 'getIpIsExist',
        success:function (result) {
            //console.log(result);
            if(result===0){
                layer.ready(function(){
                    var index = layer.load(2);
                    layer.open({
                        type: 2,
                        title: false, //不显示标题
                        anim: 2,
                        area: ['70%','90%'],
                        shadeClose: true, //开启遮罩关闭
                        content: [url,'yes']
                    });
                    layer.close(index);
                })
            }
        }
    });
});


$("#aboutUs").click(function () {
    layui.use('layer', function() {
        var layer = layui.layer;
        layer.ready(function(){
            var index = layer.load(2);
            layer.open({
                type: 2,
                title: false, //不显示标题
                anim: 2,
                area: ['70%','90%'],
                shadeClose: true, //开启遮罩关闭
                content: [url,'yes']
            });
            layer.close(index);
        })
    });
});
window.onload=function () {
    findNews();
    getAreaList();
    getRecycleData();
};
//找到新闻
function findNews() {
    $.ajax({
        type: 'get',
        dataType : 'json',
        contentType:"application/json;charset=utf-8",
        url: 'http://106.53.82.206:4000/users/news',
        success:function (data) {
            $news = $("#news");
            for(let i = 0;i<12;i++){
                var img=data[i].img.replace('https','http');
                $news.append("<div class='news-item layui-col-md3 layui-col-sm3'>\n" +
                    "                    <div class='news-img'>\n" +
                    "                        <a href='"+'https://www.thepaper.cn/newsDetail_forward_'+data[i].id+"' target=\"_blank\"><img src="+img+" alt=''></a>\n" +
                    "                    </div>\n" +
                    "                    <div class='news-body'>\n" +
                    "                        <p><a href='"+'https://www.thepaper.cn/newsDetail_forward_'+data[i].id+"' target=\"_blank\">"+data[i].title+"</a></p>\n" +
                    "                    </div>\n" +
                    "                </div>")
            }


        }
    })
}

var body = $('body');
var map =  $(".map-dot");
if ($(".map-branch")) {
    // //默认显示上海
    // $(".map-dot.shanghai").addClass("cur");
    // $("#company_shanghai").show().css({
    //     "left": $(".map-dot.shanghai").position().left + 36,
    //     "top": $(".map-dot.shanghai").position().top - $("#company_shanghai").height() / 2 + 8
    // });
    body.on('mouseover', '.map-dot', function () {
        $(this).addClass("cur").siblings(".map-dot").removeClass("cur");
    });
    body.on('click', '.map-dot', function () {
        $(this).addClass("cur").siblings(".map-dot").removeClass("cur");
        var dataid = $(this).attr("data-id")
            , comp = $("#company_" + dataid)
            , otherComp = $("#company_" + dataid).siblings(".branch-map-data");
        comp.fadeIn("fast").css({
            "left": $(this).position().left + 36,
            "top": $(this).position().top - comp.height() / 2 + 8
        });
        otherComp.hide();
    });
    body.on('mouseleave', '.branch-map-data', function () {
        //console.log("离开");
        $(this).hide();
        $(".map-dot").removeClass("cur");
    });

}

function getAreaList(){
    $.ajax({
        type:"get",
        url:"getRecyclingAreaList",
        success:function (data) {
            var Province = [];
            var city = [];
            var neighbourhood = [];
            var addTime = [];
            var ConvertPinYin = [];

            //console.log(data);

            for (var i = 0; i<data.length;i++){
                Province[i] = data[i].province;
                city[i] = data[i].city;
                addTime[i] = data[i].addTime;
                neighbourhood[i] = data[i].neighbourhood;
            }
            Province.sort();
            jQuery.uniqueSort(Province);

            var map_content = $('.map-content-box');
            var branch_map = $('.branch-map-l');
            var map_data = [];
            map_content.empty();
            //console.log(Province);
            for(var j = 0;j<Province.length;j++){
                var str=Province[j].substring(0,Province[j].length-1);
                ConvertPinYin[j] = getPinYinByName(str);
                //console.log(ConvertPinYin[j]);
                map_content.append("<span class='map-dot "+ConvertPinYin[j]+"' data-id="+ ConvertPinYin[j] +" value="+str+">"+str+"</span>");
                map_data[j] = $("<div class='branch-map-data' id='company_"+ConvertPinYin[j]+"'></tr>");
                map_data[j].appendTo(branch_map);
                var title = $("<div class='branch-map-data-title'><p>"+Province[j]+"</p></div>");
                title.appendTo(map_data[j]);
                var info = $("<div class='branch-map-data-info'></div>");
                info.appendTo(map_data[j]);
                for(var k = 0;k<data.length;k++){
                    if(data[k].province===Province[j]){
                        var branch = $("<div class='data-branch'></div>");
                        branch.appendTo(map_data[j]);
                        branch.append("<p class='data-name'>"+ neighbourhood[k] +"</p>");
                        branch.append("<p>地址："+ city[k] +"</p>");
                        branch.append("<p>开通时间："+ addTime[k] +"</p>");
                    }

                }
            }

        }
    })
}

var MyMarhq;

function tableScroll(tableid, hei, speed, len) {
    clearTimeout(MyMarhq);
    $('#' + tableid).parent().find('.tableid_').remove();
    $('#' + tableid).parent().prepend(
        '<table class="tableid_"><thead>' + $('#' + tableid + ' thead').html() + '</thead></table>'
    ).css({
        'position': 'relative',
        'overflow': 'hidden',
        'height': hei + 'px'
    });
    $(".tableid_").find('th').each(function(i) {
        $(this).css('width', $('#' + tableid).find('th:eq(' + i + ')').width());
    });
    $(".tableid_").css({
        'position': 'absolute',
        'top': 0,
        'left': 0,
        'z-index': 9
    });
    $('#' + tableid).css({
        'position': 'absolute',
        'top': 0,
        'left': 0,
        'z-index': 1
    });

    if ($('#' + tableid).find('tbody tr').length > len) {
        $('#' + tableid).find('tbody').html($('#' + tableid).find('tbody').html() + $('#' + tableid).find('tbody').html());
        $(".tableid_").css('top', 0);
        $('#' + tableid).css('top', 0);
        var tblTop = 0;
        var outerHeight = $('#' + tableid).find('tbody').find("tr").outerHeight();

        function Marqueehq() {
            if (tblTop <= -outerHeight * $('#' + tableid).find('tbody').find("tr").length) {
                tblTop = 0;
            } else {
                tblTop -= 1;
            }
            $('#' + tableid).css('margin-top', tblTop + 'px');
            clearTimeout(MyMarhq);
            MyMarhq = setTimeout(function() {
                Marqueehq()
            }, speed);
        }

        MyMarhq = setTimeout(Marqueehq, speed);
        $('#' + tableid).find('tbody').hover(function() {
            clearTimeout(MyMarhq);
        }, function() {
            clearTimeout(MyMarhq);
            if ($('#' + tableid).find('tbody tr').length > len) {
                MyMarhq = setTimeout(Marqueehq, speed);
            }
        })
    }

}